<template>
  <div class="app-container">
    <el-row style="">
      <el-col :span="12">
        <el-button type="success" @click="addlist" plain>新增</el-button>
        <el-button type="danger" @click="batchesdelete" plain
          >批量删除</el-button
        >
      </el-col>
      <el-col :span="11">
        <el-input
          v-model="ipt"
          placeholder="请输入内容"
          class="input-with-select"
        >
          <el-button
            @click="grabbleClick"
            slot="append"
            icon="el-icon-search"
          ></el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-table
      :data="list"
      border
      style="width: 100%; margin:15px 0"
      max-height="380"
      @selection-change="handleSeleveionChange"
    >
      <el-table-column fixed type="index" label="序号" width="50">
      </el-table-column>
      <el-table-column type="selection" label="#" width="120">
      </el-table-column>
      <el-table-column prop="date" label="日期" width="170" sortable>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="220"> </el-table-column>
      <el-table-column prop="author" label="作者" width="150">
      </el-table-column>
      <el-table-column prop="type" label="分类" width="200"> </el-table-column>
      <el-table-column prop="time" label="时间" width="140"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button @click="deleteClick(scope.row)" type="text" size="small"
            >删除</el-button
          >
          <el-button type="text" size="small" @click="modification(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="1"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="20"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-drawer
      :visible.sync="drawer"
      :before-close="handleClose"
      :withHeader="false"
      :direction="direction"
      size="80%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item style="margin-top:50px;" label="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item :disabled="false" label="内容">
          <mavonEditor v-model="form.content" />
        </el-form-item>
        <el-form-item style="margin-top:50px;" label="作者">
          <el-select disabled v-model="form.author">
            <el-option label="situ" value="situ"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item style="margin-top:50px;" label="type">
          <el-select v-model="form.cid" placeholder="请选择type">
            <el-option
              v-for="(item, index) in classify"
              :key="index"
              :label="item.name"
              :value="item.id"
              >{{ item.name }}</el-option
            >
          </el-select>
        </el-form-item>

        <el-form-item style="margin-top:50px;" label="time">
          <el-select v-model="form.time" placeholder="time">
            <el-option label="1分钟" :value="1"></el-option>
            <el-option label="2分钟" :value="2"></el-option>
            <el-option label="3分钟" :value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item style="margin-top:50px;">
          <el-button @click="addlists">立即{{ drawertitle }}</el-button>
          <el-button @click="countermandform">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { mapActions, mapState, mapMutations } from "vuex";
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
export default {
  data() {
    return {
      drawer: false,
      direction: "rtl",
      drawertitle: "新增",
      form: {
        author: "situ"
      },
      delearr: [],
      ipt: ""
    };
  },
  async mounted() {
    await this.getmsbddata(); //获取值渲染表格
  },
  methods: {
    ...mapActions("msbd", [
      "getmsbddata",
      "addedlist",
      "deletelist",
      "modifications"
    ]),
    ...mapMutations("msbd", ["SET_CURRENTPAGE", "SET_PAGESIZE"]),
    //搜索
    grabbleClick() {
      this.getmsbddata(this.ipt.trim());
    },
    //批量删除
    batchesdelete() {
      if (this.delearr.length) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        })
          .then(async () => {
            await this.deletelist(this.delearr.join(","));
            await this.getmsbddata();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      } else {
        alert("请选择数据");
      }
    },
    //修改
    modification(row) {
      this.drawer = true;
      this.form = row;
    },
    //多选
    handleSeleveionChange(row) {
      console.log(row)
      this.delearr = [];
      for (let a = 0; a < row.length; a++) {
        this.delearr.push(row[a].id);
      }
    },
    //删除按钮
    deleteClick(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(async () => {
          await this.deletelist(row.id);
          await this.getmsbddata();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //每页多少条
    handleSizeChange(row) {
      this.SET_PAGESIZE(row);
      this.getmsbddata();
    },
    //当前第几页
    handleCurrentChange(row) {
      this.SET_CURRENTPAGE(row - 1);
      this.getmsbddata();
    },
    //新增按钮
    addlist() {
      this.drawer = true;
    },
    //关闭遮罩层
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    //确定按钮
    async addlists() {
      if (this.form.id) {
        await this.modifications(this.form);
      } else {
        await this.addedlist(this.form);
      }
      await this.getmsbddata();
      await this.emptyform();
      this.drawer = false;
    },
    //取消按钮
    countermandform() {
      this.drawer = false;
      this.emptyform();
    },
    //清空form表单
    emptyform() {
      this.form = { author: "situ" };
    }
  },
  computed: {
    ...mapState("msbd", ["list", "classify", "total"])
  },
  components: {
    mavonEditor
  }
};
</script>

<style scoped></style>
